<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>组件基础</title>
    <!-- Delete ".min" for console warnings in development -->
    <script src="../vue.js"></script>

  </head>
  <body>

    <!-- app -->
    <div id="app">
      组件基础 — Vue.js<br />
      https://cn.vuejs.org/v2/guide/components.html<br />
      <br />
      <div :style="{ fontSize: postFontSize + 'em' }">
        <blog-post v-for="post in posts"
          v-bind:key="post.id"
          v-bind:post="post"  
          v-on:enlarge-text="onEnlargeTextaa"
        ></blog-post>
      </div>
    </div>

    <script>

// 定义新组件
Vue.component('blog-post', {
  props:['post'],
  template: `
    <div class="blog-post">
      <h3>{{post.title}}</h3>
      <button v-on:click="$emit('enlarge-text', 0.1)">
        Enlarge text
      </button>
      <div v-html="post.content"></div>
      <div>{{post.content}}</div>
    </div>
  `
})

var app = new Vue({
  el: '#app',
  data : {
    posts:[
      {
        id: 1,
        title:'标题一',
        content:'内容一'
      },
      {
        id: 2,
        title:'标题二',
        content:'内容二'
      },
      { id: 3, title: 'Why Vue is so fun' , content:'内容3'},
      { id: 4, title: '标题四' , content:'内容4<b>加粗</b>'},
    ],
    postFontSize: 1
  },
  'methods': {
    'onEnlargeTextaa':function (enlargeAmount) {
      console.log('调用onEnlargeText');
      console.log(enlargeAmount);
      if (enlargeAmount<=0) {
        enlargeAmount = 1;
      }
      this.postFontSize += enlargeAmount;
    }
  }
})


    </script>
  </body>
</html>
